# 适用的场景： 数据采集中  需要绕过登录  然后进入到某个页面
# 个人信息页面是utf-8 但还是报错  编码错误  因为没有进入到个人呢信息页面  而是跳转到了登录界面
# 登陆界面不是utf-8  所以报错

# 什么情况下访问不成功？
# 因为请求头的信息 不够  所以访问不成功


import urllib.request
import urllib.parse

url = 'https://weibo.com/u/5315202435'

headers = {
    'cookie':'XSRF-TOKEN=Of7Z-63IqdiJExK71U55vsir; _s_tentry=weibo.com; appkey=; Apache=6032506263193.629.1723806828953; SINAGLOBAL=6032506263193.629.1723806828953; ULV=1723806828959:1:1:1:6032506263193.629.1723806828953:; SCF=AsGb2J1ZWeXn8-IkX3M-OmVvCpOoxA_zlA4YZRaBhS7Tq8hzURQ6ag0zCGXffdRkX6CwcGniS3fi0GkpY2cpTlo.; SUB=_2A25Lu0WxDeRhGeNN6lcT8CzIyDmIHXVoucd5rDV8PUNbmtANLRCikW9NScBUtztqdpZ_I7CHSPReXOsLFF2toOCN; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WWNQPjwx8pgQqdofMHsoGHl5NHD95Qfe02feo5EShefWs4DqcjMi--NiK.Xi-2Ri--ciKnRi-zNSKepSKz7eoB0SBtt; ALF=02_1726399201; WBPSESS=8mUTFI3AcVqvKyKnpaP4GrLc0mutLpYtpumlN9UNMFEbJYVoRJu_NMqCEzz12WY0SbgenaXsTMHcal41VW0HLM5JtjEZ-NVDLK3733JUihHGfRvAWqhiCeCQQK99RSDe7JcY1uFDMyeDYfXpViAmJw=='
}

# 请求对象的定制
request = urllib.request.Request(url=url, headers=headers)

# 模拟浏览器向服务器发送请求
response = urllib.request.urlopen(request)

# # 获取响应的数据
# content = response.read().decode('gb2312')
#
# # print(content)
# # 将数据保存到本地
# with open('weibo.html', 'w', encoding='gb2312') as fp:
#     fp.write(content)


# 获取响应的数据
content = response.read().decode('utf-8')

print(content)
# 将数据保存到本地
with open('weibo.html', 'w', encoding='utf-8') as fp:
    fp.write(content)

